//
//  AboutUsViewController.m
//  WJTheWishPro
//
//  Created by appleLJ on 2018/1/19.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "AboutUsViewController.h"
#import "AboutUsTableViewCell.h"
#import "SystemHelpTools.h"
#import "WebPageViewController.h"

@interface AboutUsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) UIView *headerView;
@property(nonatomic,strong) UIView *footerView;
@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong) SystemHelpTools *systemHelpTools;
@end

@implementation AboutUsViewController
- (void)systemHelpList {
    [self.dataArray removeAllObjects];
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=getarticlelist" parameters:@{@"articlecat":self.typeStr} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"error_code"] isEqualToString:@"0"]) {
            self.dataArray = responseObject[@"data"];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    self.title = [self.typeStr isEqualToString:@"1"]?@"关于帮你":@"用户指南";
    [self systemHelpList];
    
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AboutUsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AboutUsTableViewCell class]) forIndexPath:indexPath];
    cell.dataDic = self.dataArray[indexPath.row];
    //添加底线
    if (indexPath.row != self.dataArray.count -1) {
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = getColor(@"F2F2F2");
        [cell.contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).offset(10 / WIDTH_5S_SCALE);
            make.right.equalTo(cell.contentView).offset(-10 / WIDTH_5S_SCALE);
            make.bottom.equalTo(cell.contentView);
            make.height.mas_equalTo(1);
        }];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44 / WIDTH_5S_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WebPageViewController *VC = [[WebPageViewController alloc]init];
    VC.dataDic = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark -- init

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = getColor(@"F2F2F2");
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = YES;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerClass:[AboutUsTableViewCell class] forCellReuseIdentifier:NSStringFromClass([AboutUsTableViewCell class])];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 8)];
        _headerView.backgroundColor = getColor(@"F2F2F2");
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}
- (SystemHelpTools *)systemHelpTools
{
    if (!_systemHelpTools) {
        _systemHelpTools = [[SystemHelpTools alloc]init];
        
    }
    return _systemHelpTools;
}
@end
